﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Skill.Ode.Geometries;
using Skill.Ode;
using Skill.Ode.Extension;

namespace Samples.Common
{
    public class Entity<T> : IDisposable where T : DGeometry
    {
        public T Geometry { get; protected set; }
        public DWorld World { get; private set; }
        public DisplayGeometry Display { get; set; }

        #region Constructor
        public Entity(DWorld world, T geometry)
        {
            if (world == null)
                throw new ArgumentNullException("World of entity can not be null");
            if (geometry == null)
                throw new ArgumentNullException("Geometry of entity can not be null");
            this.World = world;
            this.Geometry = geometry;
            Display = null;
        }

        #endregion

        public void UpdateDisplay()
        {
            if (Display != null)
                Display.Transform = Geometry.WorldMatrix.ToXna();
        }

        #region IDisposable Members

        public virtual void Dispose()
        {
            if (Geometry != null)
                Geometry.Dispose();
            Geometry = null;
            World = null;
        }

        #endregion
    }

    public class MovableEntity<T> : Entity<T> where T : DGeometry
    {
        public DBody Body { get; protected set; }        

        #region Constructor
        public MovableEntity(DWorld world, T geometry)
            : base(world, geometry)
        {
            Body = new DBody(World);
            Geometry.Body = Body;
        }

        #endregion

        #region IDisposable Members
        public override void Dispose()
        {
            if (Body != null)
                Body.Dispose();
            Body = null;
            base.Dispose();
        }

        #endregion
    }    
}
